package com.newtype.meeting.client.widget;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.model.Degree;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.model.MeetingRecord;
import com.newtype.meeting.client.model.MeetingReport;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.client.service.DepartmentServiceAsync;
import com.newtype.meeting.client.service.MeetingCategoryServiceAsync;
import com.newtype.meeting.client.service.TitleServiceAsync;
import com.newtype.meeting.shared.DepartmentTreeDTO;

public class TestPanel extends ContentPanel {

	public TestPanel() {
		setHeading("Test");
		setSize(400, 500);

		final ListStore<Title> titles = new ListStore<Title>();
		titles.add(new ArrayList<Title>());

		final ComboBox<Title> combo4 = new ComboBox<Title>();
		combo4.setFieldLabel("职称");
		combo4.setEmptyText("请选择职称 ...");
		combo4.setEditable(false);
		combo4.setTriggerAction(TriggerAction.ALL);
		combo4.setDisplayField("titleCaption");
		combo4.setStore(titles);

		final ListStore<Degree> degrees = new ListStore<Degree>();
		final TitleServiceAsync titleService = Registry
				.get(GMeeting.TITLE_SERVICE);
		titleService.getDegrees(new AsyncCallback<ArrayList<Degree>>() {
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Error");
			}

			@Override
			public void onSuccess(ArrayList<Degree> result) {
				degrees.add(result);
			}
		});

		ComboBox<Degree> combo3 = new ComboBox<Degree>();
		combo3.setFieldLabel("职级");
		combo3.setEmptyText("请选择职级...");
		combo3.setEditable(false);
		combo3.setTriggerAction(TriggerAction.ALL);
		combo3.setDisplayField("degreeID");
		combo3.setStore(degrees);
		combo3
				.addSelectionChangedListener(new SelectionChangedListener<Degree>() {
					@Override
					public void selectionChanged(
							final SelectionChangedEvent<Degree> se) {
						combo4.clearSelections();
						combo4.getStore().removeAll();
						final List<Title> list = new ArrayList<Title>();
						titleService
								.getTitles(new AsyncCallback<ArrayList<Title>>() {
									@Override
									public void onFailure(Throwable caught) {
										System.out.println("Error2");
									}

									@Override
									public void onSuccess(
											ArrayList<Title> result) {
										Iterator<Title> iter = result
												.iterator();
										while (iter.hasNext()) {
											Title title = iter.next();
											if (title.get("degree") == se
													.getSelectedItem().get(
															"degreeID")) {
												list.add(title);
											}
										}
										titles.add(list);
									}
								});
					}
				});

		add(combo3);
		add(combo4);

		final SimpleComboBox<String> combo2 = new SimpleComboBox<String>();
		combo2.setFieldLabel("职称");
		combo2.setEmptyText("请选择一个职称...");
		combo2.setEditable(false);
		combo2.setTriggerAction(TriggerAction.ALL);

		final SimpleComboBox<Integer> combo1 = new SimpleComboBox<Integer>();
		combo1.setFieldLabel("职级");
		combo1.setEmptyText("请选择一个职级...");
		combo1.setTriggerAction(TriggerAction.ALL);
		combo1.setEditable(false);
		titleService.getDegrees(new AsyncCallback<ArrayList<Degree>>() {
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("-->Error");
			}

			@Override
			public void onSuccess(ArrayList<Degree> result) {
				for (Degree d : result) {
					combo1.add(d.getDegreeID());
				}
			}
		});
		combo1
				.addSelectionChangedListener(new SelectionChangedListener<SimpleComboValue<Integer>>() {
					@Override
					public void selectionChanged(
							final SelectionChangedEvent<SimpleComboValue<Integer>> se) {
						combo2.clearSelections();
						combo2.removeAll();

						titleService
								.getTitles(new AsyncCallback<ArrayList<Title>>() {
									@Override
									public void onFailure(Throwable caught) {

									}

									@Override
									public void onSuccess(
											ArrayList<Title> result) {
										Iterator<Title> iter = result
												.iterator();
										while (iter.hasNext()) {
											Title title = iter.next();
											if (title.get("degree") == se
													.getSelectedItem()
													.getValue()) {
												combo2.add(title
														.getTitleCaption());
											}
										}
									}
								});
					}
				});
		add(combo1);
		add(combo2);
		testOrganizationTree();
		testMeetingCategoryTree();
		testHtmlEditor();

		addButton(getReportButton());
		addButton(getRecordButton());
		addButton(getMultiButton());
		addButton(getEditOpenTypeButton());
		addButton(getNoticeButton());
		addButton(getSeperateButton());
		addButton(getEditVcardButton());

	}

	private Button getSeperateButton() {
		return new Button("选取公开对象", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToOrganizationSeperateWindow);
			}
		});
	}

	private Button getNoticeButton() {
		return new Button("会议通知", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToMeetingNotice,
						new Meeting());
			}
		});
	}

	private Button getMultiButton() {

		return new Button("选择其他部门", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeMultiWindow);
			}
		});
	}

	private Button getRecordButton() {
		return new Button("维护会议记录", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToRecordWindow,
						new MeetingRecord());
			}
		});
	}

	private Button getReportButton() {
		return new Button("登录会议报告", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToReportWindow,
						new MeetingReport());
			}
		});
	}

	private Button getEditOpenTypeButton() {
		return new Button("修改会议公开对象", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToEditOpenTypeWindow,
						new Meeting());
			}
		});
	}

	private Button getEditVcardButton() {
		return new Button("修改我的资料", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToVcardEdit);
			}
		});
	}

	public void testHtmlEditor() {
		final FormPanel fp = new FormPanel();
		fp.setHeading("Comments/Feedback");
		fp.setFrame(true);
		fp.setLabelAlign(LabelAlign.TOP);
		fp.setLabelSeparator("");

		final HtmlEditor ed = new HtmlEditor();
		ed.setFieldLabel("Please Enter Your Comments/Feedback");
		ed.setHeight(200);
		fp.add(ed, new FormData("100%"));

		fp.setButtonAlign(HorizontalAlignment.RIGHT);
		Button submit = new Button("Submit");
		fp.addButton(submit);
		SelectionListener<ButtonEvent> listener = new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				System.out.println(ed.getValue());
			}
		};
		submit.addSelectionListener(listener);
		fp.add(new Button("Cancel"));

		fp.setBounds(10, 10, 600, 300);
		fp.layout();
		add(fp);
	}

	public void testOrganizationTree() {
		DepartmentServiceAsync deptService = Registry
				.get(GMeeting.DEPARTMENT_SERVICE);
		final TreeStore<ModelData> store = new TreeStore<ModelData>();

		deptService.getTreeModel(new AsyncCallback<DepartmentTreeDTO>() {
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("提示", "error", null);
			}

			@Override
			public void onSuccess(DepartmentTreeDTO result) {
				DepartmentTreeDTO model = result;
				store.add(model.getChildren(), true);
			}
		});
		final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
		tree.setWidth(300);
		tree.setDisplayProperty("name");

		ButtonBar buttonBar = new ButtonBar();

		buttonBar.add(new Button("Expand All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.expandAll();
					}
				}));
		buttonBar.add(new Button("Collapse All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.collapseAll();
					}
				}));

		add(buttonBar, new FlowData(10));
		add(tree, new FlowData(10));
	}

	public void testMeetingCategoryTree() {
		final TreeStore<ModelData> store = new TreeStore<ModelData>();

		MeetingCategoryServiceAsync categoryService = Registry
				.get(GMeeting.CATEGORY_SERVICE);
		categoryService.getCategoryTree(new AsyncCallback<MeetingCategory>() {
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("提示", "error", null);
			}

			@Override
			public void onSuccess(MeetingCategory result) {
				store.add(result.getChildren(), true);
			}
		});

		final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
		tree.setWidth(300);
		tree.setDisplayProperty("categoryName");

		add(tree, new FlowData(10));
	}

}
